package com.hainatong.shiphelper.module.warehouse.viewmodel;

import android.app.Application;
import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import com.hainatong.shiphelper.api.ApiResult;
import com.hainatong.shiphelper.api.ApiService;
import com.hainatong.shiphelper.api.VMObserver;
import com.hainatong.shiphelper.module.warehouse.entity.ShelfUuidBean;
import com.hainatong.shiphelper.module.warehouse.entity.ShipWareHouseBean;
import com.hainatong.shiphelper.module.warehouse.entity.WareShelfBean;
import com.hainatong.mvvmbase.base.BaseViewModel;
import com.hainatong.mvvmbase.base.LiveResult;
import com.hainatong.mvvmbase.http.RxSchedulersUtils;
import io.reactivex.disposables.Disposable;

import java.util.List;

public class WareHouseViewModel extends BaseViewModel
{

    public WareHouseViewModel(@NonNull Application application)
    {
        super(application);
    }


    private MutableLiveData<LiveResult<ShelfUuidBean>> shelfUuidResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<ShelfUuidBean>> getShelfUuidResult()
    {
        return shelfUuidResult;
    }

    /**
     *  通过 UUid 查询 货架 信息
     * @param uuid
     */
    public void queryWareHouseShelfInfoByUUid(String uuid)
    {
        ApiService.Builder.getChanBoServer()
                          .queryWareHouseByUUid(uuid)
                          .compose(RxSchedulersUtils.Obs_io_main())
                          .subscribe(new VMObserver<ApiResult<ShelfUuidBean>>() {
                              @Override
                              public void onStart(Disposable d)
                              {
                                addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getShelfUuidResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<ShelfUuidBean> result)
                              {
                                  if( result.isOk() )
                                  {
                                      getShelfUuidResult().postValue(new LiveResult<>( true , result.getData()));
                                  }
                                  else
                                  {
                                      getShelfUuidResult().postValue(new LiveResult<>(false , result.getMessage()));
                                  }
                              }
                          });

    }

    private MutableLiveData<LiveResult<List<ShipWareHouseBean>>> shipWareHouseResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<ShipWareHouseBean>>> getShipWareHouseResult()
    {
        return shipWareHouseResult;
    }

    /**
     *  查询 仓库
     * @param shipId
     */
    public void queryWareHouseByShipId(String shipId)
    {
        ApiService.Builder.getChanBoServer()
                .queryWareHouseByShipId(shipId)
                .compose(RxSchedulersUtils.Obs_io_main())
                .subscribe(new VMObserver<ApiResult<List<ShipWareHouseBean>>>() {
                    @Override
                    public void onStart(Disposable d)
                    {
                        addDisposable( d );
                    }

                    @Override
                    public void onError(int code, String errorMsg)
                    {
                        getShipWareHouseResult().postValue(new LiveResult<>(false , errorMsg));
                    }

                    @Override
                    public void onSuccess(ApiResult<List<ShipWareHouseBean>> result)
                    {
                        //{"data":[{"wareHouseName":"物资仓库","count":0}],"status":true,"code":"200","message":"操作成功"}
                       if( result.isOk() )
                       {
                           getShipWareHouseResult().postValue(new LiveResult<>( true , result.getData()));
                       }
                       else
                       {
                           getShipWareHouseResult().postValue(new LiveResult<>(false , result.getMessage()));
                       }
                    }
                });
    }


    private MutableLiveData<LiveResult<List<WareShelfBean>>> shelfResult = new MutableLiveData<>();

    public MutableLiveData<LiveResult<List<WareShelfBean>>> getShelfResult()
    {
        return shelfResult;
    }

    /**
     *  查询 货架
     * @param name
     */
    public void queryShelfByWareName(String name)
    {
        ApiService.Builder.getChanBoServer()
                          .queryShelfByWareHouseName( name )
                          .compose( RxSchedulersUtils.Obs_io_main() )
                          .subscribe(new VMObserver<ApiResult<List<WareShelfBean>>>()
                          {
                              @Override
                              public void onStart(Disposable d)
                              {
                                  addDisposable( d );
                              }

                              @Override
                              public void onError(int code, String errorMsg)
                              {
                                  getShelfResult().postValue( new LiveResult<>( false ,errorMsg ) );
                              }

                              @Override
                              public void onSuccess(ApiResult<List<WareShelfBean>> result)
                              {
                                  //{"data":[{"children":[{"uuid":"20e3c954-e86c-4e75-b7ff-f054f561b92f","code":"NO6-002"}],"shelfName":"1号货架","floors":"2"}],"status":true,"code":"200","message":"操作成功"}
                                  if( result.isOk() )
                                  {
                                      getShelfResult().postValue( new LiveResult<>( true , result.getData() ) );
                                  }
                                  else
                                  {
                                      getShelfResult().postValue( new LiveResult<>( false , result.getMessage() ) );
                                  }
                              }
                          });

    }


}
